<?php


namespace App\Http\Searchs\Product;


use Illuminate\Database\Query\Builder;
use LTools\Searchs\SearchAbstract;

class InventorySearch extends SearchAbstract
{

    /**
     * 关系映射.
     *
     * @return array
     */
    protected function relationship(): array
    {
        return [
            'product_code' => 'closure',
            'customer_id' => '=',
        ];
    }

    /**
     * @param $code
     * @return \Closure
     * @author luffyzhao@vip.126.com
     */
    protected function getProductCodeAttribute($code){
        return function (Builder $builder) use ($code){
            $builder->whereIn('product_id',function(Builder $builder) use ($code){
                $builder->from('products')->select(['id']);
                if(!empty($code)){
                    $builder->where('code', '=', $code);
                }
            });
        };
    }
}
